﻿var sqlHelper = require('./../common/SqlHelper');
var md5 = require('md5');
var users = require('./../common/UserCache.js');
var message = require('./../handler/message');

module.exports = {
    handlerName: 'authorize',

    //验证
    /*        
        返回信息
        {
            msg:'返回消息',
            isAuthorized:true,  //是否授权
        }
    */
    validate: function (data, callback) {
        var me = this,
            obj = {
                success: false,
                detail: ''
            };

        //查询授权
        try {
            var params = {
                appId: parseInt(data.appId)
            }

            sqlHelper.exec('dbo.pQueryAuthorizationKey', params, function (err, result) {
                if (err) {
                    obj.msg = err.message;
                    callback(obj);
                } else {
                    var record = result.recordset;

                    if (record.length > 0) {
                        //appId + key + 时间 生成md5
                        var code = [data.appId, record[0].Key, data.time];
                        var enCode = md5(code.join('_'));
                        if (enCode == data.md5) {
                            obj.success = true;
                            obj.detail = '效验成功';

                        } else {
                            obj.detail = 'md5信息不匹配';
                        }
                    } else {
                        obj.detail = '未找到匹配的信息,请联系管理员';
                    }
                    callback(obj);
                }
            });
        } catch (e) {
            Console.log(e);
        }
    }
}